<?php

if(!defined("INCLUDE")) die("Nedostatečné oprávnění pro spouštění souboru.");

# --------------------------------------------------------------------------------------------------------------------------
# CLASS ENTITY
# --------------------------------------------------------------------------------------------------------------------------
# Třída entity se používá jako přepravní kontejner pro data o článku. Je zde řešeno ohlídání proti SQL injekci, některé
# get metody také upravujou výstup dat, čili třeba z datumu lze získat vlastní formát.
#
# --------------------------------------------------------------------------------------------------------------------------
# INFORMACE O SOUBORU
# --------------------------------------------------------------------------------------------------------------------------
# Autor: Zechy
# Poslední editace: 1. 12. 2012

class entity
{
	private $id;
	private $autor;
	private $category;
	private $heading;
	private $perex;
	private $content;
	private $datetime;
	private $status;
	private $tags;

	private $sql;

	public function __construct()
	{
		$this->sql = new sqlquery;
	}

	public function setAll_fromDB($data)
	{
		if(isset($data["id"])) $this->id = $data["id"];

		$this->autor = $data["autor"];
		$this->category = $data["category"];
		$this->heading = $data["heading"];
		$this->perex = $data["perex"];
		$this->content = $data["content"];
		$this->tags = $data["tags"];

		if(isset($data["datetime"])) $this->datetime = $data["datetime"];
		if(isset($data["status"])) $this->status = $data["status"];
	}

	public function setAll($data)
	{
		$this->sql->create_connect();

		if(isset($data["id"])) $this->id = mysql_real_escape_string($data["id"]);
		$this->autor = mysql_real_escape_string($data["autor"]);
		$this->category = mysql_real_escape_string($data["category"]);
		$this->heading = mysql_real_escape_string($data["heading"]);

		if(strpos($data["content"], "<!-- pagebreak -->"))
		{
			$content = explode("<!-- pagebreak -->", $data["content"]);
			$this->perex = mysql_real_escape_string($content[0]);
			$this->content = mysql_real_escape_string($content[1]);
		}
		else
		{
			$this->content = mysql_real_escape_string($data["content"]);
		}

		$this->tags = mysql_real_escape_string($data["tags"]);
		if(empty($data["date"]) && empty($data["time"])) $this->datetime = date("Y-m-d H:i:s");
		else if(isset($data["date"]) && isset($data["time"])) $this->datetime = $data["date"]." ".$data["time"];

		if(isset($data["public"])) $this->status = 1;
		if(isset($data["save"])) $this->status = 0;

		$this->sql->close_connect();
	}

	public function set_id($id)
	{
		$this->sql->create_connect();

		$this->id = mysql_real_escape_string($id);

		$this->sql->close_connect();
	}
	public function set_autor($autor)
	{
		$this->sql->create_connect();

		$this->autor = mysql_real_escape_string($autor);

		$this->sql->close_connect();
	}
	public function set_category($category)
	{
		$this->sql->create_conect();

		$this->category = mysql_real_escape_string($category);

		$this->sql->close_conect();
	}
	public function set_heading($heading)
	{
		$this->sql->create_conect();

		$this->heading = mysql_real_escape_string($heading);

		$this->sql->close_conect();
	}
	public function set_content($content)
	{
		$this->sql->create_conect();

		$content = explode("<!-- pagebreak -->", $content);

		$this->perex = mysql_real_escape_string($content[0]);
		$this->content = mysql_real_escape_string($content[1]);

		$this->sql->close_conect();
	}
	public function set_datetime($datetime)
	{
		$this->sql->create_conect();

		$this->datetime = mysql_real_escape_string($datetime);

		$this->sql->close_conect();
	}
	public function set_status($status)
	{
		$this->sql->create_conect();

		$this->status = mysql_real_escape_string($status);

		$this->sql->close_conect();
	}
	public function set_tags($tags)
	{
		$this->sql->create_conect();

		$this->tags = mysql_real_escape_string($tags);

		$this->sql->close_conect();
	}
	public function get_id()
	{
		return $this->id;
	}
	public function get_autor()
	{
		return $this->autor;
	}
	public function get_category()
	{
		return $this->category;
	}
	public function get_heading()
	{
		return $this->heading;
	}
	public function get_content($what = "both")
	{
		if($what == "both" && !empty($this->perex))
		{
			return $this->perex."<!-- pagebreak -->".$this->content;
		}
		else if($what == "content" || ($what == "both" && empty($this->perex)))
		{
			return $this->content;
		}
		else if($what == "perex")
		{
			return $this->perex;
		}
	}
	public function get_datetime($what = "both", $format = null)
	{
		$formated;
		
		if(!is_null($format))
		{
			$formated = date($format, strtotime($this->datetime));
		}

		if($what == "both")
		{
			return $this->datetime;
		}
		else if($what == "date")
		{
			$datetime = explode(" ", $this->datetime);
			return $datetime[0];
		}
		else if($what == "time")
		{
			$datetime = explode(" ", $this->datetime);
			return $datetime[1];
		}
		else if($what == "formated")
		{
			return $formated;
		}
	}
	public function get_status()
	{
		return $this->status;
	}

	public function get_tags()
	{
		return $this->tags;
	}
}

?>